﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Edgar.Entities.Characteristics;
using Edgar.Entities.MasterData;
using System.Windows;

namespace Edgar.Frontend.Wpf.ViewModels
{
    public class TalentViewModel : INotifyPropertyChanged
    {
        private readonly CharacterTalent _characterTalent;
        private readonly Rulebook _rulebook;
        private readonly int _characterCircle;

        public TalentViewModel(Rulebook rulebook, CharacterTalent characterTalent, int characterCircle, int attributeStep)
        {
            _rulebook = rulebook;
            _characterTalent = characterTalent;
            _characterCircle = characterCircle;
            AttributeStep = attributeStep;
        }

        public string Name { get { return _characterTalent.Name; } }

        public int Rank
        {
            get { return _characterTalent.Rank; }
            set
            {
                _characterTalent.Rank = value;
                NotifyPropertyChanged("Rank");
                NotifyPropertyChanged("Step");
                NotifyPropertyChanged("Dice");
                NotifyPropertyChanged("TalentPointsLeft");
            }
        }

        public string AttributeName
        {
            get
            {
                Talent t = _rulebook.Talents.Find(x => x.Name == _characterTalent.Name);
                return t == null ? "TALENT NOT MATCHED!" : t.Attribute;
            }
        }
        public int AttributeStep { get; set; }
        public int Step { get { return Rank > 0 ? AttributeStep + Rank : 0; } }
        public string Dice { get { return Rank > 0 ? _rulebook.GetDice(Step) : ""; } }

        public int Circle
        {
            get
            {
                return _characterTalent.Circle;
            }
        }

        public Visibility HiddenIfNoAttribute { get { return string.IsNullOrEmpty(AttributeName) ? Visibility.Hidden : Visibility.Visible; } }

        public bool IsTalentAvailable { get { return Circle <= _characterCircle; } }

        #region Implementing INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string info)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(info));
        }

        #endregion
    }
}
